package top.forgirl.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import top.forgirl.common.result.Result;
import top.forgirl.common.result.ResultType;
import top.forgirl.domain.Company;
import top.forgirl.domain.dto.CompanyDo;
import top.forgirl.service.CompanyService;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import java.util.List;

@RestController
@Api(tags = "公司管理模块")
@Validated
@CrossOrigin
public class CompanyController {

    @Autowired
    private CompanyService companyService;

    @ApiOperation("查询所有的公司列表")
    @GetMapping(value = "/company/list")
    public Result<List<Company>> listCompany() {
        List<Company> companyList = companyService.listCompany();
        return new Result<>(companyList);
    }

    @ApiOperation("删除公司信息")
    @DeleteMapping("/company")
    public Result<Company> deleteCompany(@RequestBody @NotBlank String cId) {

        Company result = companyService.deleteCompany(cId);

        if (result != null) {
            return new Result<>(result);
        } else {
            return new Result<>(4001, "未查找该公司信息", null);
        }
    }

    @ApiOperation("添加公司信息")
    @PostMapping("/company")
    public Result<CompanyDo> addCompany(@Valid @RequestBody CompanyDo companyDo) {
        CompanyDo returnCompanyDo = companyService.addCompany(companyDo);
        return new Result<>(returnCompanyDo);
    }

    @ApiOperation("修改公司信息")
    @PutMapping("/company")
    public Result updateCompany(@RequestBody Company company) {
        Company result = companyService.updateCompany(company);
        if (result != null) {
            return new Result(result);
        } else {
            return new Result(ResultType.FAILED, "更新失败");
        }
    }

    @ApiOperation("通过relation查询公司信息")
    @GetMapping("/company/{relation}")
    public Result select(@PathVariable @NotBlank(message = "查询条件中关系不能为空") String relation) {
        Company company = companyService.selectCompanyByRelation(relation);
        return new Result(company);
    }

}
